package com.teachoa.web;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.teachoa.bean.Msg;
import com.teachoa.bean.User;
import com.teachoa.bean.UserMsg;
import com.teachoa.enumtype.MsgTypeEnum;
import com.teachoa.enumtype.UserTypeEnum;
import com.teachoa.util.IDUtil;

public class MsgController extends BaseController {
	
	public void index() {
		int pageNo = getParaToInt("pageNo", 1);
		int isProMsg = getParaToInt("isProMsg", 0);
		Page<Msg> msgPage = Msg.dao.findMsgsByPage(pageNo, isProMsg);
		setAttr("msgPage", msgPage);
		renderJsp("msg.jsp");
	}
	
	public void addMsg() {
		setAttr("teachers", User.dao.findUserByType(UserTypeEnum.TEACHER.getCode()));
		setAttr("students", User.dao.findUserByType(UserTypeEnum.STUDENT.getCode()));
		setAttr("experts", User.dao.findUserByType(UserTypeEnum.EXPERT.getCode()));
		renderJsp("msg_add.jsp");
	}
	
	@Before(Tx.class)
	public void saveMsg() {
		
		Msg msg = getModel(Msg.class);
		
		String msgId = IDUtil.getId();
		msg.set("id", msgId);
		msg.set("status", 0);
		msg.set("create_time", new Date());
		
		int type = msg.getInt("type");
		try {
			if(type==MsgTypeEnum.COMMON.getCode()) {
				String[] userIds = getParaValues("userIds");
				for(String id:userIds) {
					UserMsg um = new UserMsg();
					um.set("id", IDUtil.getId());
					um.set("msg_id", msgId);
					um.set("user_id", id);
					um.save();
				}
			} 
			msg.save();
			renderJson(success("保存成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(success("保存失败！！"));
		}
	}
	
	@Before(Tx.class)
	public void publish() {
		try {
			Msg.dao.publish(getPara());
			renderJson(success("发布成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("发布失败！！"));
		}
	}
	
	public void delMsg() {
		try {
			Msg.dao.deleteById(getPara());
			renderJson(success("删除成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(fail("删除失败！！"));
		}
	}
	
	public void loadMsg() {
		String msgId = getPara();
		setAttr("msg", Msg.dao.findById(msgId));
		List<UserMsg> list = UserMsg.dao.getMsgUsers(msgId);
		String[] ids = new String[list.size()];
		for(int i=0;i<list.size();i++) {
			ids[i] = list.get(i).getStr("user_id");
		}
		setAttr("userIds", StringUtils.join(ids, ","));
		setAttr("teachers", User.dao.findUserByType(UserTypeEnum.TEACHER.getCode()));
		setAttr("students", User.dao.findUserByType(UserTypeEnum.STUDENT.getCode()));
		setAttr("experts", User.dao.findUserByType(UserTypeEnum.EXPERT.getCode()));
		String oper = getPara("oper");
		if("view".equals(oper))
			renderJsp("msg_view.jsp");
		else 
			renderJsp("msg_add.jsp");
	}
	
	@Before(Tx.class)
	public void update() {
		Msg msg = getModel(Msg.class);
		try {
			msg.update();
			int type = msg.getInt("type");
			if(type==MsgTypeEnum.COMMON.getCode())
				UserMsg.dao.updateRelation(msg.getStr("id"), getParaValues("userIds"));
			renderJson(success("保存成功！！"));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(success("保存失败！！"));
		}
	}

}
